package com.tool.btn;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.SwingConstants;

import com.updateNew.MyIsif;
import org.come.Frame.AlchemyJframe;
import org.come.Frame.OptionsJframe;
import org.come.Frame.PetEquipmentJframe;
import org.come.Frame.RolePetResistanceJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.*;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.ChosePetLxMouslisten;
import org.come.mouslisten.ChosePetSkillsMouslisten;
import org.come.mouslisten.GoodsMouslisten;
import org.come.mouslisten.PetAddPointMouslisten1;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.Article;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.SendRoleAndRolesummingUntil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import com.tool.PanelDisplay.PetPanelShow1;
import com.tool.imagemonitor.FightingMonitor;
import com.tool.pet.PetProperty;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;

import come.tool.Fighting.FightingMixDeal;
import come.tool.Fighting.Fightingimage;
import come.tool.Fighting.TypeState;
import come.tool.JDialog.TiShiUtil;
import come.tool.handle.HandleState;

/**
 * 召唤兽面板按钮
 *
 * @author Administrator
 */
public class PetPanelBtn1 extends MoBanBtn {
    private TestPetJpanel1 jpanel;
    /**
     * 1是改名 2是参战 3休息 4加点 5驯养 6技能 7物品 8炼妖 9 放生 10抗性 11 召唤
     */
    private Integer typeBtn;

    public PetPanelBtn1(String iconpath, int type, Integer typeBtn, TestPetJpanel1 jpanel) {
        // TODO Auto-generated constructor stub
        super(iconpath, type);
        // this.setText(text);
        this.typeBtn = typeBtn;

        setFont(UIUtils.TEXT_FONT1);
        setForeground(Color.yellow);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.jpanel = jpanel;
    }

    public PetPanelBtn1(String iconpath, int type, Color[] colors, Font font, String text, Integer typeBtn, TestPetJpanel1 jpanel) {
        // TODO Auto-generated constructor stub
        super(iconpath, type, colors);
        this.setText(text);
        setFont(font);
        this.typeBtn = typeBtn;
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.jpanel = jpanel;
    }


    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        if (UserMessUntil.getChosePetMes() == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("您还没有选中召唤兽呢!!");
            return;
        }
        // TODO Auto-generated method stub
        if (typeBtn == 1 && FightingMixDeal.State == HandleState.USUAL) {
            changname();
        } else if (typeBtn == 2 && FightingMixDeal.State == HandleState.USUAL) {
            canzhan();
        } else if (typeBtn == 3 && FightingMixDeal.State == HandleState.USUAL) {
            try {
                xiuxi();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (typeBtn == 4 && FightingMixDeal.State == HandleState.USUAL) {
            try {
                jiadian();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (typeBtn == 5 && FightingMixDeal.State == HandleState.USUAL) {
            xunyang();
        } else if (typeBtn == 6) {
            ChosePetSkillsMouslisten.refreshPetSkills();
            // 展示技能面板
            FormsManagement.showForm(18);
        } else if (typeBtn == 601) {
            ChosePetLxMouslisten.refreshPetSkills();
            // 展示灵犀面板
            FormsManagement.showForm(601);
        } else if (typeBtn == 7 && FightingMixDeal.State == HandleState.USUAL) {
            ZhuJpanel.setUseGoodsType(1);
            PetEquipmentJframe.getPetEquipmentJframe().getEquipmentJpanel().showPet(UserMessUntil.getChosePetMes());
            FormsManagement.HideForm(2);
            FormsManagement.upgradForm(67);
            PetEquipmentJpanel.starRating = UserMessUntil.getChosePetMes().getFragment();//召唤兽升星
        } else if (typeBtn == 8 && FightingMixDeal.State == HandleState.USUAL) {
            lianyao();
        } else if (typeBtn == 9 && FightingMixDeal.State == HandleState.USUAL) {
            /**判断是否解锁*/
            if (Util.isCanBuyOrno()) {
                return;
            }
            if (UserMessUntil.getChosePetMes() == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("没有选中的召唤兽");
                return;
            }
            OptionsJframe.getOptionsJframe().getOptionsJpanel().
                    showBox(TiShiUtil.Release, UserMessUntil.getChosePetMes(), "#W确定要将召唤兽:#G" + UserMessUntil.getChosePetMes().getSummoningname() + "#W放生吗?");
        } else if (typeBtn == 10) {
            petZhanshi();
        } else if (typeBtn == 20) {//宠物跟随
            changePet(true);
        } else if (typeBtn == 21) {//跟随隐藏
            changePet(false);
        } else if (FightingMixDeal.State == HandleState.HANDLE_PLAYER && typeBtn == 11) {
            Fightingimage fightingimage = FightingMixDeal.getdata(0);
            List<TypeState> data = fightingimage.getFightingManData().cxxx("召唤兽");
            int sid = UserMessUntil.getChosePetMes().getSid().intValue();
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).getState() != 0) {
                    continue;
                }
                if (sid == Integer.parseInt(data.get(i).getType())) {
                    if (FightingMixDeal.State != HandleState.HANDLE_PLAYER) {
                        return;
                    }
                    FormsManagement.HideForm(666);
                    // 生成召唤指令
                    FightingMonitor.FightingOperation(FightingBtn.SpellGenerate("召唤&" + data.get(i).getType()));
                    FightingMonitor.operateEnd();
                    return;
                }
            }
            ZhuFrame.getZhuJpanel().addPrompt2("这只召唤兽无法召唤");

        }
    }

    /**
     * 改名
     */
    public void changname() {
        // 将更改的名字进行传送到服务器进行修改数据
        try {
            if (UserMessUntil.getChosePetMes() != null) {
                String lastname = UserMessUntil.getChosePetMes().getSummoningname();
                // 若要修改的名字为空，则提示其输入新的召唤兽名字
                if (TestPetJpanel1.getLabname().getText() == null || "".equals(TestPetJpanel1.getLabname().getText())) {

                    ZhuFrame.getZhuJpanel().addPrompt("请输入召唤兽" + lastname + "的新名字！");
                    return;
                }
                if (TestPetJpanel1.getLabname().getText().length() > 9) {
                    ZhuFrame.getZhuJpanel().addPrompt("长?那是一种专属");
                    return;
                }
                // 改变名字
                UserMessUntil.getChosePetMes().setSummoningname(TestPetJpanel1.getLabname().getText().trim());


                // 发送给服务器
                String mes = Agreement.getAgreement().petchangeAgreement(GsonUtil.getGsonUtil().getgson().toJson(UserMessUntil.getChosePetMes()));
                // 向服务器发送信息
                SendMessageUntil.toServer(mes);
                ZhuFrame.getZhuJpanel().addPrompt2("您的召唤兽改名为:" + UserMessUntil.getChosePetMes().getSummoningname());
                // 刷新面板

                TestPetJpanel1.showStar();
            } else {
                ZhuFrame.getZhuJpanel().addPrompt("请选择你要改名的召唤兽！");
            }
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    /**
     * 参战
     */
    public void canzhan() {
        // 将宠物头像框设置为有头像
        String path = "img/newHead/p" + UserMessUntil.getChosePetMes().getSummoningskin() + ".png";
        ZhuJpanel.setLabpetimg(path);
//        ZhuJpanel.setLabpetimg(UserMessUntil.getChosePetMes().getSummoningskin(),1);
        // 选中的召唤兽id
        String mes = Agreement.getAgreement().rolechangeAgreement("P" + UserMessUntil.getChosePetMes().getSid().toString());
        SendMessageUntil.toServer(mes);
        // 设置自身携带的宠物id
        RoleData.getRoleData().getLoginResult().setSummoning_id(UserMessUntil.getChosePetMes().getSid());
        // 绘制召唤兽 3条
        Article.souxie(UserMessUntil.getChosePetMes());
        PetPanelShow1.ShowMesForJpanel1();
        jpanel.getBtnwar().setText("休息");
        jpanel.getBtnwar().setColors(UIUtils.COLOR_BLACK);

        jpanel.getBtnwar().setTypeBtn(3);
        TestPetJpanel1.showStar();
    }

    /**
     * 休息
     */
    public void xiuxi() throws Exception {
        ZhuJpanel.setLabpetimg(null);
        String mes = Agreement.getAgreement().rolechangeAgreement("P");
        SendMessageUntil.toServer(mes);
        RoleData.getRoleData().getLoginResult().setSummoning_id(null);
        jpanel.getBtnwar().setText("参战");
        jpanel.getBtnwar().setColors(UIUtils.COLOR_BLACK);

        jpanel.getBtnwar().setTypeBtn(2);
        TestPetJpanel1.showStar();
    }

    /**
     * 加点
     */
    public void jiadian() throws Exception {
        RoleSummoning pet = UserMessUntil.getChosePetMes();
        int Jbone = Integer.valueOf(TestPetJpanel1.getLabrootbone().getText());
        int JSpir = Integer.valueOf(TestPetJpanel1.getLabintelligence().getText());
        int JPower = Integer.valueOf(TestPetJpanel1.getLabpower().getText());
        int JSpeed = Integer.valueOf(TestPetJpanel1.getLabspeed().getText());
        int JCalm = Integer.valueOf(TestPetJpanel1.getLabconcentrate().getText());
        int canpoint = pet.getCanpoint();
        canpoint -= Jbone;
        canpoint -= JSpir;
        canpoint -= JPower;
        canpoint -= JSpeed;
        canpoint -= JCalm;

        canpoint += pet.getZBone();
        canpoint += pet.getZSpir();
        canpoint += pet.getZPower();
        canpoint += pet.getZSpeed();
        canpoint += pet.getZCalm();
        if (canpoint >= 0
                && (Jbone > pet.getZBone() || JSpir > pet.getZSpir() || JPower > pet.getZPower()
                || JSpeed > pet.getZSpeed() || JCalm > pet.getZCalm())) {
            // 设置根骨数目
            pet.setZBone(Jbone);
            // 设置最新灵性
            pet.setZSpir(JSpir);
            // 设置最新力量
            pet.setZPower(JPower);
            // 设置敏捷度
            pet.setZSpeed(JSpeed);
            // 设置定力
            pet.setZCalm(JCalm);
            // 增加对应的属性
            PetAddPointMouslisten1.showPetValue();
            // 发送给服务器
            String mes = Agreement.getAgreement().petchangeAgreement(GsonUtil.getGsonUtil().getgson().toJson(pet));
            // 向服务器发送信息
            SendMessageUntil.toServer(mes);
        }
    }

    /**
     * 驯养
     */
    public void xunyang() {
        // 选中了召唤兽，然后把宠物口粮进行解析
        int a = 0;
        for (int i = 0; i < GoodsListFromServerUntil.getGoodslist().length; i++) {
            if (UserMessUntil.getChosePetMes().getFaithful() >= 100) {
                ZhuFrame.getZhuJpanel().addPrompt2(
                        "召唤兽 " + UserMessUntil.getChosePetMes().getSummoningname() + "的忠诚度已满！！！");
                return;
            }
            if (GoodsListFromServerUntil.getGoodslist()[i] != null
                    && GoodsListFromServerUntil.getGoodslist()[i].getType() == 49) {
                UserMessUntil.getChosePetMes().addFaithful(
                        Integer.parseInt(GoodsListFromServerUntil.getGoodslist()[i].getValue()));
                ZhuFrame.getZhuJpanel().addPrompt2(
                        "召唤兽 " + UserMessUntil.getChosePetMes().getSummoningname() + "增加了"
                                + GoodsListFromServerUntil.getGoodslist()[i].getValue() + "点忠诚度啦！！！");
                GoodsListFromServerUntil.getGoodslist()[i].goodxh(1);
                GoodsMouslisten.gooduse(GoodsListFromServerUntil.getGoodslist()[i], 1);
                if (GoodsListFromServerUntil.getGoodslist()[i].getUsetime() <= 0) {
                    GoodsListFromServerUntil.Deleted(i);
                }
                // 将这个召唤兽发给服务器
                SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
                // 刷新界面忠诚度
                TestPetJpanel1.getLabloyalty().setText(UserMessUntil.getChosePetMes().getFaithful().toString());
                a = 1;
            }
        }
        if (a == 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("您没有足够宠物口粮了！！！");
        }
    }

    /**
     * 炼妖
     */
    public void lianyao() {
        // 清空左上角放置召唤兽的表格
        AlchemyJframe.getAlchemyjframe().getalchemyJpanel().getListModel().removeAllElements();
        // 将自己的召唤兽列表放到界面上
        if (UserMessUntil.getPetListTable() != null) {
            for (int i = 0; i < UserMessUntil.getPetListTable().size(); i++) {
                AlchemyJframe.getAlchemyjframe().getalchemyJpanel().getListModel().add(i, UserMessUntil.getPetListTable().get(i).getSummoningname());
            }
        }
        // 将界面展示出来
        FormsManagement.upgradForm(17);

    }

    /**
     * 召唤兽抗性面板展示
     */
    public void petZhanshi() {
        if (UserMessUntil.getChosePetMes() == null) {
            // 显示在聊天面板
            FrameMessageChangeJpanel.addtext(5, "请选择你要查看抗性的召唤兽！", null, null);
        } else {
            // 展示召唤兽抗性面板
            if (!FormsManagement.getframe(58).isVisible()) {
                PetProperty.ShowQl(UserMessUntil.getChosePetMes());
                FormsManagement.showForm(58);
            } else {
                FormsManagement.HideForm(58);
            }

        }
    }

    /**
     * 进行遍历展示召唤兽抗性
     */
    public static void showRolesumming(Object model) {
        int a = -1, doub = -1, jishu = -1;
        try {
            clearShuXingView();
            for (java.lang.reflect.Field field : model.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                if (Double.parseDouble(field.get(model).toString()) != 0) {
                    String sx = null;
                    sx = JpanelOnJalbelBtn.getQuaralyPersonalName(field.getName());
                    // //system.out.println("值： " + sx);
                    String ping = GoodsMsgJpanel.tianjia(sx);
                    String numbers = sx.substring(sx.length() - 1, sx.length());
                    Integer number = Integer.valueOf(numbers);
                    // Integer number = Integer.valueOf(1);
                    String shuxingName = sx.substring(0, sx.length() - 2);
                    a++;
                    if (a % 2 == 0) {
                        doub++;
                        RolePetResistanceJframe.getResistancejframe().getResistancejpanel();
                        if ("%".equals(ping)) {
                            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[number - 1]
                                    .getDlm().addElement(
                                    shuxingName
                                            + ":"
                                            + String.format("%.1f",
                                            Double.parseDouble(field.get(model).toString())) + ping);
                        } else {
                            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[number - 1]
                                    .getDlm().addElement(
                                    shuxingName + ":" + (int) Double.parseDouble(field.get(model).toString())
                                            + ping);
                        }
                    } else {
                        jishu++;
                        RolePetResistanceJframe.getResistancejframe().getResistancejpanel();
                        if ("%".equals(ping)) {
                            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[number - 1]
                                    .getDlm1().addElement(
                                    shuxingName
                                            + ":"
                                            + String.format("%.1f",
                                            Double.parseDouble(field.get(model).toString())) + ping);
                        } else {
                            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[number - 1]
                                    .getDlm1().addElement(
                                    shuxingName + ":" + (int) Double.parseDouble(field.get(model).toString())
                                            + ping);
                        }
                    }
                }
            }
            changViewSize();
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 更新抗性面板大小
     */
    public static void changViewSize() {
        if ("水墨UI".equals(MyIsif.getStyle())) {
            for (int i = 0; i < RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel().length; i++) {
                int leftNum = RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i]
                        .getDlm().getSize();
                int rightNum = RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i]
                        .getDlm1().getSize();
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getListNo1()
                        .setBounds(20, 26, 130, leftNum * 17);
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getListNo2()
                        .setBounds(150, 26, 130, rightNum * 17);
                int num = leftNum > rightNum ? leftNum : rightNum;
                num = num > 0 ? (num * 17 + 34) : 24;
                int y = 0;
                for (int j = 0; j < i; j++) {
                    y += RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[j]
                            .getHeight();
                }
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].setBounds(0, y,
                        300, num);
            }
            int y = 0;
            for (int i = 0; i < RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel().length; i++) {
                y += RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getHeight();
            }
            // RoleResistanceJframe.getResistancejframe().getResistancejpanel().setBounds(RoleResistanceJframe.getResistancejframe().getResistancejpanel().getX(),RoleResistanceJframe.getResistancejframe().getResistancejpanel().getY(),290,y);
            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().setSize(300, y);
            RolePetResistanceJframe.getResistancejframe().setSize(300, y);
        } else {
            for (int i = 0; i < RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel().length; i++) {
                int leftNum = RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i]
                        .getDlm().getSize();
                int rightNum = RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i]
                        .getDlm1().getSize();
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getListNo1()
                        .setBounds(20, 26, 130, leftNum * 17);
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getListNo2()
                        .setBounds(150, 26, 130, rightNum * 17);
                int num = leftNum > rightNum ? leftNum : rightNum;
                num = num > 0 ? (num * 17 + 34) : 24;
                int y = 0;
                for (int j = 0; j < i; j++) {
                    y += RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[j]
                            .getHeight();
                }
                RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].setBounds(0, y,
                        290, num);
            }
            int y = 0;
            for (int i = 0; i < RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel().length; i++) {
                y += RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getHeight();
            }
            // RoleResistanceJframe.getResistancejframe().getResistancejpanel().setBounds(RoleResistanceJframe.getResistancejframe().getResistancejpanel().getX(),RoleResistanceJframe.getResistancejframe().getResistancejpanel().getY(),290,y);
            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().setSize(290, y);
            RolePetResistanceJframe.getResistancejframe().setSize(290, y);
        }
    }

    /**
     * 清空抗性面板内容
     */
    public static void clearShuXingView() {
        for (int i = 0; i < RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel().length; i++) {
            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getDlm().removeAllElements();
            RolePetResistanceJframe.getResistancejframe().getResistancejpanel().getShuXingJpanel()[i].getDlm1().removeAllElements();
        }
    }

    private void changePet(boolean b) {

        if (b && RoleData.getRoleData().getLoginResult().getSummoning_id() != null && RoleData.getRoleData().getLoginResult().getSummoning_id().compareTo(UserMessUntil.getChosePetMes().getSid()) == 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("参战召唤兽不可以观看！");
            return;
        }

        // 改变名字
        UserMessUntil.getChosePetMes().setShow(b);
        // 发送给服务器
        String mes = Agreement.getAgreement().petchangeAgreement(
                GsonUtil.getGsonUtil().getgson().toJson(UserMessUntil.getChosePetMes()));
        // 向服务器发送信息
        SendMessageUntil.toServer(mes);
        // 刷新面板
        TestPetJpanel1.showStar();
    }

    public Integer getTypeBtn() {
        return typeBtn;
    }

    public void setTypeBtn(Integer typeBtn) {
        this.typeBtn = typeBtn;
    }

}
